package com.kele.test.rabbitmq.controller;

import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageDeliveryMode;
import org.springframework.amqp.core.MessageProperties;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("producer05")
public class ProducerController_High5 {

    @Autowired
    RabbitTemplate rabbitTemplate;

    @GetMapping("sendMessage_direct")
    public String sendMessage_direct(){

        rabbitTemplate.convertAndSend("exchange_direct","q1_1905A","Hello 1905A");

        return "OK";
    }
    @GetMapping("sendMessage_fanout")
    public String sendMessage_fanout(){

        rabbitTemplate.convertAndSend("exchange_fanout","sssss","Hello 1905A");

        return "OK";
    }

    @GetMapping("sendMessage_topic/{routingKey}")
    public String sendMessage_topic(@PathVariable String routingKey){

        rabbitTemplate.convertAndSend("exchange_topic",routingKey,"Hello 1905A");

        return "OK";
    }

    @GetMapping("sendMessage_headers")
    public String sendMessage_headers(){
        /**
         * 声明消息 (消息体, 消息属性)
         */
        MessageProperties messageProperties = new MessageProperties();
        // 设置消息是否持久化。Persistent表示持久化，Non-persistent表示不持久化
        messageProperties.setDeliveryMode(MessageDeliveryMode.NON_PERSISTENT);
        messageProperties.setContentType("text/plain");
        messageProperties.setContentEncoding("UTF-8");

        //messageProperties.getHeaders().put("type","1905A.email");
        messageProperties.getHeaders().put("type","1905A.sms");

        Message message = new Message("Hello 1905A！".getBytes(), messageProperties);

        rabbitTemplate.convertAndSend("exchange_headers","",message);

        return "OK";
    }

    @GetMapping("sendMessage_headers1")
    public String sendMessage_headers1(){

        rabbitTemplate.convertAndSend("exchange_headers",
                "",
                "Hello 1905A",(message)->{
                    MessageProperties messageProperties = message.getMessageProperties();
                    messageProperties.setDeliveryMode(MessageDeliveryMode.NON_PERSISTENT);
                    messageProperties.setContentType("text/plain");
                    messageProperties.setContentEncoding("UTF-8");

                    //messageProperties.getHeaders().put("type","1905A.email");
                    messageProperties.getHeaders().put("type","1905A.email");

                    return message;
                });

        return "OK";
    }
}
